package com.sky.financelake.ui.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;
import com.sky.financelake.R;
import com.sky.financelake.adapter.SimplePlateZjlxAdapter;
import com.sky.financelake.base.BaseToolA;
import com.sky.financelake.bean.dto.SimplePlateZjlxDto;
import com.sky.financelake.databinding.ActivityPlateZjlxBinding;
import com.sky.financelake.viewmodels.remote.ZjlxViewModel;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

/**
 * 资金流向
 */
@AndroidEntryPoint
public class PlateZjlxA extends BaseToolA {

    private ActivityPlateZjlxBinding binding;

    private ZjlxViewModel zjlxViewModel;
    private List<SimplePlateZjlxDto> simplePlateZjlxDtos = new ArrayList<>();
    private SimplePlateZjlxAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityPlateZjlxBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Toolbar toolbar = binding.toolbarContainer.toolbar;
        initToolbar(toolbar, getString(R.string.zjlx));

        zjlxViewModel = new ViewModelProvider(this).get(ZjlxViewModel.class);

        AppCompatEditText etPlate = binding.etPlate;

        AppCompatTextView btnSearch = binding.tvSearch;
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = etPlate.getText().toString();
                if (text.length() == 0) {
                    Snackbar.make(etPlate, R.string.no_input_code_or_name, Snackbar.LENGTH_SHORT).show();
                } else {
                    zjlxViewModel.getPlateZjlx(text);
                }
            }
        });
        adapter = new SimplePlateZjlxAdapter(simplePlateZjlxDtos, this);
        RecyclerView rvStockZjlx = binding.rvZjlx;
        rvStockZjlx.setLayoutManager(new LinearLayoutManager(this));
        rvStockZjlx.setAdapter(adapter);

        ConstraintLayout noDataView = binding.containerNoData.noDataContainer;

        zjlxViewModel.getPlateZjlxLiveData().observe(this, response -> {
            simplePlateZjlxDtos.clear();
            List<SimplePlateZjlxDto> tmp = response.getResult();
            if (tmp.size() == 0) {
                noDataView.setVisibility(View.VISIBLE);
            } else {
                noDataView.setVisibility(View.GONE);
            }
            simplePlateZjlxDtos.addAll(tmp);
            adapter.notifyDataSetChanged();
        });

    }


}
